package commonInfrastructure.accountManagement.session;

import frm.billingAndPaymentProcessing.entity.DunningEmailEntity;
import java.util.Properties;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.PasswordAuthentication;
import scm.rawMaterialPurchasing.entity.RawMaterialPurchasingOrderEntity;

@Stateless
@LocalBean
public class EmailSessionBean {

    String emailServerName = "smtp.gmail.com";

    public EmailSessionBean() {
    }

    public void emailInitialPassward(String toEmailAdress, String initialPassword) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("melionfood.m04", "weloveyoutwk");
                    }
                });

        try {



            Message message = new MimeMessage(session);
//            message.setFrom(new InternetAddress("melionfood.m04@gmail.com"));
            message.setFrom(new InternetAddress("WeLoveTWK@MerlionFood.com"));

            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toEmailAdress));
            message.setSubject("Initial Password");
            message.setText("Greetings from Melion Food limited!"
                    + "\n\n Here is your initial password:" + initialPassword);


            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    public void sendDunningEmail(String toEmailAddress, String emailContent, DunningEmailEntity dunningEmailEntity) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("melionfood.m04", "weloveyoutwk");
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("melionfood.m04@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toEmailAddress));

            message.setSubject("Dunning Email");
//			message.setText("Dear Customer" + dunningEmailEntity.getInvoice().getDeliveryOrder().getParentSalesOrder().getCustomer().getName() + " : \n\n" );
            System.out.print("Test1....." + emailContent);

            emailContent = emailContent.replaceAll("<p>", "").replaceAll("</p>", "\n").replaceAll("<br/>", "\n");
            System.out.print("Test2....." + emailContent);
            message.setText(emailContent);
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    public void sendRawMaterialPurchasingEmail(String toEmailAddress, String emailContent) {

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("melionfood.m04", "weloveyoutwk");
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("melionfood.m04@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toEmailAddress));

            message.setSubject("Merlion Food Raw Material Purchasing Order");


            message.setText(emailContent);
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
